package com.hg.library.http;

import android.os.SystemClock;
import android.util.Log;

import com.hg.library.config.HttpConfig;

import java.io.IOException;
import java.net.ConnectException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by ASUS-PC on 2016/6/17.
 */
public class HttpInterceptor implements Interceptor {
    private static final String TAG = "HttpInterceptor";
    @Override
    public Response intercept(Chain chain) throws IOException{
        Request request = chain.request();
        Response response = null;
        try {
            response = chain.proceed(request);
            int i = 0;
            while (response == null || !response.isSuccessful() && i < HttpConfig.RETIY_TIME) {
                Log.e(TAG, "intercept: 重试第"+i);
                i++;
                SystemClock.sleep(HttpConfig.RETIY_DELAY);
                response = chain.proceed(request);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        if(response == null){
          throw new IOException("response null");
        }
        return response;
    }
}
